#include "t_excel.h"
#include <QGlobalStatic>
#include <QDebug>
#include <QFileDialog>
#include "common.h"
#include "T_PaintWidget.h"

QMap<QString,BlockInfo> g_block_map{};
Q_GLOBAL_STATIC(T_Excel,s_instance)

T_Excel::T_Excel(QObject *parent)
    : QObject{parent}
{

}

T_Excel::~T_Excel()
{

}

T_Excel *T_Excel::Instance()
{
    return s_instance;
}

void T_Excel::ReadExcel(const QString &excel_path)
{
    g_block_map.clear();


    if(excel_path.isEmpty()) {
        return;
    }
    //获取表格，选择表单，获取大小
    QXlsx::Document xlsx(excel_path);
    if(xlsx.sheetNames().isEmpty()) {
        return;
    }
    xlsx.selectSheet(xlsx.sheetNames().at(0));
    QXlsx::CellRange range = xlsx.dimension();


    int column_count = range.columnCount();
    int row_coumt = range.rowCount() - 1; //去掉表头一行
    //插入表头
    for(int i=0; i< column_count; ++i){
        if(xlsx.cellAt(1,i+1)) {
            QString tmp_head =  xlsx.cellAt(1,i+1)->value().toString();
            // qDebug() << tmp_head;
            // head.append(tmp_head);
        }
    }


    //插入内容
    for(int row=0; row< row_coumt; ++row) {
        if(xlsx.cellAt(row+2,1)->value().toString().isEmpty()) {
            continue;
        }
        BlockInfo info;
        info.type = xlsx.cellAt(row+2,1)->value().toString();
        info.author = xlsx.cellAt(row+2,2)->value().toString();
        info.jgtype = xlsx.cellAt(row+2,3)->value().toString();
        info.txtype = xlsx.cellAt(row+2,4)->value().toString();
        info.kWh = xlsx.cellAt(row+2,5)->value().toDouble();
        info.voltage = xlsx.cellAt(row+2,6)->value().toDouble();
        info.outStopVol = xlsx.cellAt(row+2,7)->value().toDouble();
        info.inStopVol = xlsx.cellAt(row+2,8)->value().toDouble();
        info.outContinue = xlsx.cellAt(row+2,9)->value().toString();
        info.out10 = xlsx.cellAt(row+2,10)->value().toString();
        info.inContinue = xlsx.cellAt(row+2,11)->value().toString();
        info.inTemp = xlsx.cellAt(row+2,12)->value().toString();
        info.outTemp = xlsx.cellAt(row+2,13)->value().toString();
        info.weight = xlsx.cellAt(row+2,14)->value().toInt();
        info.length = xlsx.cellAt(row+2,15)->value().toDouble();
        info.width = xlsx.cellAt(row+2,16)->value().toDouble();
        info.height = xlsx.cellAt(row+2,17)->value().toDouble();
        info.recircle = xlsx.cellAt(row+2,18)->value().toInt();
        g_block_map[info.type] = info;
    }
}

void T_Excel::WriteExcel()
{
    QString excel_path = QFileDialog::getSaveFileName(nullptr,tr("Save File"),
                                                      "/home",
                                                      tr("Excel File (*.xlsx)"));

    if(excel_path.isEmpty()) {
        return;
    }

    //设置行列宽高
    m_xlsx.setRowHeight(1,50);
    m_xlsx.setRowHeight(2,25);
    m_xlsx.setRowHeight(3,25);
    m_xlsx.setRowHeight(4,25);
    m_xlsx.setRowHeight(5,25);
    m_xlsx.setRowHeight(6,25);
    m_xlsx.setRowHeight(7,25);
    m_xlsx.setRowHeight(8,25);
    m_xlsx.setRowHeight(9,25);
    m_xlsx.setRowHeight(10,25);
    m_xlsx.setRowHeight(11,25);
    m_xlsx.setRowHeight(12,25);
    m_xlsx.setRowHeight(13,25);
    m_xlsx.setRowHeight(14,25);
    m_xlsx.setRowHeight(15,25);
    m_xlsx.setRowHeight(16,25);
    m_xlsx.setRowHeight(17,25);
    m_xlsx.setRowHeight(18,25);
    m_xlsx.setRowHeight(19,25);
    m_xlsx.setRowHeight(20,25);
    m_xlsx.setRowHeight(21,25);
    m_xlsx.setRowHeight(22,200);

    m_xlsx.setColumnWidth(1,20);
    m_xlsx.setColumnWidth(2,60);
    m_xlsx.setColumnWidth(3,60);
    m_xlsx.setColumnWidth(4,60);
    m_xlsx.setColumnWidth(5,60);

    //设置格式    
    m_format.setFontColor(Qt::black);
    m_format.setFontBold(true);   //加粗
    m_format.setFontSize(30);
    m_format.setFontItalic(false);    //倾斜
    m_format.setFontName("楷体");     //字体
    m_format.setPatternBackgroundColor(QColor(255,255,255,125));  //单元格背景色
    m_format.setHorizontalAlignment(QXlsx::Format::AlignHCenter); //水平居中
    m_format.setVerticalAlignment(QXlsx::Format::AlignVCenter);  //垂直居中
    m_format.setBorderColor(QColor(0,0,0));    //边框颜色

    //合并单元格
    QXlsx::CellRange cellRange(1,1,1,5);
    m_xlsx.mergeCells(cellRange,m_format);
    //写入数据
    m_xlsx.write(1,1,"电池项目技术方案可行性评估");

    m_format.setFontSize(25);
    m_format.setPatternBackgroundColor(QColor(190,177,170,125));
    m_xlsx.mergeCells("A2:A3",m_format);
    m_xlsx.mergeCells("B2:B3",m_format);
    m_xlsx.mergeCells("C2:C3",m_format);
    m_xlsx.mergeCells("D2:E2",m_format);

    m_xlsx.write(2,1,"序号");
    m_xlsx.write(2,2,"类别");
    m_xlsx.write(2,3,"项目");
    m_xlsx.write(2,4,"评估输出");
    m_xlsx.write(3,4,"需求匹配",m_format);
    m_xlsx.write(3,5,"备注",m_format);

    m_format.setFontBold(false);
    m_format.setFontSize(20);
    m_format.setPatternBackgroundColor(QColor(237,237,237,125));
    for(int i = 4; i < 23; i++)
    {
        m_xlsx.write(i,1,QString::number(i-3),m_format);
    }

    m_xlsx.mergeCells("B4:B7",m_format);
    m_xlsx.mergeCells("B8:B20",m_format);

    m_xlsx.write(4,2,"电池单体",m_format);
    m_xlsx.write(8,2,"电池系统",m_format);
    m_xlsx.write(21,2,"可行性评估总结",m_format);
    m_xlsx.write(22,2,"布置图",m_format);

    m_xlsx.write(4,3,"电芯型号",m_format);
    m_xlsx.write(5,3,"电芯体系",m_format);
    m_xlsx.write(6,3,"额定容量@0.33C",m_format);
    m_xlsx.write(7,3,"额定电压@0.33C",m_format);
    m_xlsx.write(8,3,"标称电压",m_format);
    m_xlsx.write(9,3,"电压范围",m_format);
    m_xlsx.write(10,3,"串并方案",m_format);
    m_xlsx.write(11,3,"额定容量@0.33C",m_format);
    m_xlsx.write(12,3,"总能量@0.33C",m_format);
    m_xlsx.write(13,3,"电芯总重量",m_format);
    m_xlsx.write(14,3,"持续放电功率",m_format);
    m_xlsx.write(15,3,"10s峰值放电功率",m_format);
    m_xlsx.write(16,3,"持续充电功率",m_format);
    m_xlsx.write(17,3,"充电温度",m_format);
    m_xlsx.write(18,3,"放电温度",m_format);
    m_xlsx.write(19,3,"电池尺寸",m_format);
    m_xlsx.write(20,3,"其他",m_format);

    m_xlsx.mergeCells("C21:E21",m_format);
    m_xlsx.mergeCells("C22:E22",m_format);

    writeData();

    m_xlsx.saveAs(excel_path);
}

void T_Excel::writeData()
{
    m_format.setFontColor(Qt::black);
    m_xlsx.write(4,4,g_blockInfo.type,m_format);
    m_xlsx.write(5,4,g_blockInfo.txtype,m_format);
    m_xlsx.write(6,4,QString("%1Ah").arg(g_blockInfo.kWh),m_format);
    m_xlsx.write(7,4,QString("%1V").arg(g_blockInfo.voltage),m_format);
    m_xlsx.write(8,4,g_resultInfo.standerdVol,m_format);
    m_xlsx.write(9,4,g_resultInfo.voltageRange,m_format);
    m_xlsx.write(10,4,g_resultInfo.c_b_way,m_format);
    m_xlsx.write(11,4,QString("%1Ah").arg(g_blockInfo.kWh),m_format);
    m_xlsx.write(12,4,g_resultInfo.sumkWh,m_format);
    m_xlsx.write(13,4,g_resultInfo.sumWeight,m_format);
    m_xlsx.write(14,4,g_resultInfo.continueOuterKW,m_format);
    m_xlsx.write(15,4,g_resultInfo.outer_10_kw,m_format);
    m_xlsx.write(16,4,g_resultInfo.continueInterKW,m_format);
    m_xlsx.write(17,4,QString("%1℃").arg(g_blockInfo.inTemp),m_format);
    m_xlsx.write(18,4,QString("%1℃").arg(g_blockInfo.outTemp),m_format);
    m_xlsx.write(19,4,g_resultInfo.pack_l_w_h,m_format);
    m_xlsx.write(20,4,"/",m_format);

    m_format.setFontColor(Qt::red);
    m_xlsx.write(21,3,g_resultInfo.result,m_format);

    g_resultInfo.image = T_PaintWidgetIns->GetImg();
    m_xlsx.insertImage(22,3,g_resultInfo.image);
}
